package patrol.mobile.com.mobilepatrol.activity.route_query;

import android.content.Intent;
import android.content.res.Resources;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.TypedValue;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

import patrol.mobile.com.mobilepatrol.R;
import patrol.mobile.com.mobilepatrol.activity.BaseActivity;
import patrol.mobile.com.mobilepatrol.fragment.RouteListFragment;
import patrol.mobile.com.mobilepatrol.fragment.SubOfficeListFragment;
import patrol.mobile.com.mobilepatrol.utils.CommonUtil;

import static patrol.mobile.com.mobilepatrol.utils.CommonUtil.dip2px;


/**
 * Created by apple on 18/1/9.
 */

public class TotalRouteActivity extends BaseActivity {
    public int from;

    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_total_route;
    }

    @Override
    public void setListener() {

    }
    private TabLayout tabLayout;
    private ViewPager mViewPager;
    private FragmentPagerAdapter fAdapter;

    private List<Fragment> list_fragment;
    private List<String> list_title;

    private SubOfficeListFragment water;
    private RouteListFragment elec;

    @Override
    public void initView() {
        super.initView();
        from = getIntent().getIntExtra("from",-1);
        tabLayout = (TabLayout)findViewById(R.id.tab_title);
        mViewPager = (ViewPager)findViewById(R.id.vp_pager);

        //初始化各fragment
        water = new SubOfficeListFragment();
        elec = new RouteListFragment();

        //将fragment装进列表中
        list_fragment = new ArrayList<>();
        list_fragment.add(water);
        list_fragment.add(elec);

        //将名称加载tab名字列表，正常情况下，我们应该在values/arrays.xml中进行定义然后调用
        list_title = new ArrayList<>();
        list_title.add("按分局");
        list_title.add("公路类型");

        //设置TabLayout的模式
        tabLayout.setTabMode(TabLayout.MODE_FIXED);
        //为TabLayout添加tab名称
        tabLayout.addTab(tabLayout.newTab().setText(list_title.get(0)));
//        tabLayout.addTab(tabLayout.newTab().setText(list_title.get(1)));

        fAdapter = new RouteListAdapter(getSupportFragmentManager(),list_fragment,list_title);

        mViewPager.setAdapter(fAdapter);
        tabLayout.setupWithViewPager(mViewPager,false);
//        reflex(tabLayout);
    }
    @Override
    public void initTitle() {
        super.initTitle();

        iv_back.setImageResource(R.mipmap.up_white);
        iv_back.setVisibility(View.VISIBLE);
        iv_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                CommonUtil.hiddenKeyBoard(TotalRouteActivity.this);
                CommonUtil.exitActivityAndBackAnim(TotalRouteActivity.this,true);
            }
        });
        tv_center_title.setVisibility(View.VISIBLE);
        tv_center_title.setText("管养路线");


    }
    public void reflex(final TabLayout tabLayout){
        //了解源码得知 线的宽度是根据 tabView的宽度来设置的
        tabLayout.post(new Runnable() {
            @Override
            public void run() {
                try {
                    //拿到tabLayout的mTabStrip属性
                    LinearLayout mTabStrip = (LinearLayout) tabLayout.getChildAt(0);

                    int dp10 = dip2px(tabLayout.getContext(), 10);

                    for (int i = 0; i < mTabStrip.getChildCount(); i++) {
                        View tabView = mTabStrip.getChildAt(i);

                        //拿到tabView的mTextView属性  tab的字数不固定一定用反射取mTextView
                        Field mTextViewField = tabView.getClass().getDeclaredField("mTextView");
                        mTextViewField.setAccessible(true);

                        TextView mTextView = (TextView) mTextViewField.get(tabView);

                        tabView.setPadding(0, 0, 0, 0);

                        //因为我想要的效果是   字多宽线就多宽，所以测量mTextView的宽度
                        int width = 0;
                        width = mTextView.getWidth();
                        if (width == 0) {
                            mTextView.measure(0, 0);
                            width = mTextView.getMeasuredWidth();
                        }

                        //设置tab左右间距为10dp  注意这里不能使用Padding 因为源码中线的宽度是根据 tabView的宽度来设置的
                        LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) tabView.getLayoutParams();
                        params.width = width ;
                        params.leftMargin = dp10;
                        params.rightMargin = dp10;
                        tabView.setLayoutParams(params);

                        tabView.invalidate();
                    }

                } catch (NoSuchFieldException e) {
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            }
        });

    }
    public void setIndicator(TabLayout tabs, int leftDip, int rightDip) {
        Class<?> tabLayout = tabs.getClass();
        Field tabStrip = null;
        try {
            tabStrip = tabLayout.getDeclaredField("mTabStrip");
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        }

        tabStrip.setAccessible(true);
        LinearLayout llTab = null;
        try {
            llTab = (LinearLayout) tabStrip.get(tabs);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }

        int left = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, leftDip, Resources.getSystem().getDisplayMetrics());
        int right = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, rightDip, Resources.getSystem().getDisplayMetrics());

        for (int i = 0; i < llTab.getChildCount(); i++) {
            View child = llTab.getChildAt(i);
            child.setPadding(0, 0, 0, 0);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.MATCH_PARENT, 1);
            params.leftMargin = left;
            params.rightMargin = right;
            child.setLayoutParams(params);
            child.invalidate();
        }


    }
    @Override
    public void onStart() {
        super.onStart();
        tabLayout.post(new Runnable() {
            @Override
            public void run() {
                setIndicator(tabLayout, 50, 50);
            }
        });
    }
    public void sendData(RouteManage rm){
        switch (from){
            case 1:
                break;
            case 2:
                Intent intent = new Intent();
                intent.putExtra("route",rm);
                setResult(1,intent);
                CommonUtil.exitActivityAndBackAnim(TotalRouteActivity.this,true);
                break;
        }

    }
}
